<?php
namespace Home\Controller;
class InventoryInformationController extends CommonController{


	protected $table="inventory_information";
  protected $pageSize = 12;
  
  
  public function index(){
  	$res = D('OfficeStationery')->where(array('info_status'=>1,'belongs_department'=>D('ApplicationForm')->getSelfDepartment()))->select();
	$getalldepartments_url = "Department/getAllDepartments";
    $alldepartments = json_decode(http_get($getalldepartments_url),true);
  	$this->assign('type',arr_to_map($res,type,true));
	$this->assign('name',arr_to_map($res,name,true));
	$this->assign('belongsDepartment',D('ApplicationForm')->getSelfDepartments());
	$this->assign('departName',D('ApplicationForm')->getSelfDepartment());
  	$this->display();
  }


public function getList(){
  	
		
	$this->setQeuryCondition();
    $count = (int)$this->M->count();
    $this->setQeuryCondition();
    $list = $this->M->page(I('get.p',0). ','. $this->pageSize)->order('id desc')->select();
	foreach ($list as $k => $v) {
    	$list[$k]['price']=M('storage_detail')->where(array('name'=>$v['name'],'specification'=>$v['specification'],'unit'=>$v['unit']))->getField('price');
    }
	
	foreach ($list as $k => $v) {
      $list[$k]['total_value']=$list[$k]['total_num'] * $list[$k]['price'] ;
	  $list[$k]['total_value'] = sprintf("%.2f", $list[$k]['total_value']);
    }
	
    $result = array('list'=>$list, 'count'=>$count);
	$this->ajaxReturn($result);
  }

//设置查询
  public function setQeuryCondition(){
  	if (isset($_GET['belongs_department'])) {
		$this->M->where(array("belongs_department"=> array('EQ', I('get.belongs_department'))));
	}else{
		$this->M->where(array("belongs_department"=> array('EQ', D('ApplicationForm')->getSelfDepartment())));
	}

	if (I('get.type')) {
		$this->M->where(array("type"=> array('EQ', I('get.type'))));
	}
	
	if (I('get.name')) {
		$this->M->where(array("name"=> array('like', "%".I('get.name')."%")));
	}


  }

  public function getData(){
    $list = D('InventoryInformation')->distinct(true)->where(array('info_status'=>1,'belongs_department'=>D('ApplicationForm')->getSelfDepartment()))->getField('name',true);
    $this->ajaxReturn($list);
  }

  public function getSpecByName($name){
    $list = D('InventoryInformation')->distinct(true)->where(array('info_status'=>1,'belongs_department'=>D('ApplicationForm')->getSelfDepartment(),'name'=>$name))->getField('specification',true);
    $this->ajaxReturn($list);
  }
  public function getUnitsBySpec($specification,$name){
    $list = D('InventoryInformation')->distinct(true)->where(array('info_status'=>1,'belongs_department'=>D('ApplicationForm')->getSelfDepartment(), 'specification'=>$specification, 'name'=>$name))->getField('unit',true);
    $this->ajaxReturn($list);
  }
  
  public function getMaxNum($unit,$name,$specification){
    $list = D('InventoryInformation')->distinct(true)->where(array('info_status'=>1,'belongs_department'=>D('ApplicationForm')->getSelfDepartment(), 'unit'=>$unit, 'name'=>$name,'specification'=>$specification))->getField('inventory_num',true);
    $this->ajaxReturn($list);
  }
  /**
   * 暂时想不到更好的办法，先就这样了
   */
  public function getChangeDetail($name,$specification,$unit,$belongs_department=null,$p=1,$start1=null,$start2=null){
  	if(!$belongs_department){
  		$department_id = D('UserInfo')->where(array('realname'=>$_SESSION['account']['userInfo']['realname']))->getField('department_id');
		if($department_id===0){
			$belongs_department = '总部';
	    }else{
	  	    $belongs_department = D('Department')->where(array('id'=>$department_id))->getField('name');
	    } 
  	}
	
	$list = array();
	$list1= array();
	$res1 = D('storage_detail')->where(array('info_status'=>1,'name'=>$name,'specification'=>$specification,'unit'=>$unit,'belongs_department'=>$belongs_department))->select();
	foreach ($res1 as $k => $v) {
      $list1[$k]['status']='入库';
	  $list1[$k]['time']=$res1[$k]['order_time'];
	  $list1[$k]['name']=$res1[$k]['name'];
	  $list1[$k]['specification']=$res1[$k]['specification'];
	  $list1[$k]['unit']=$res1[$k]['unit'];
	  $list1[$k]['num']=$res1[$k]['num'];
	  $list1[$k]['app_name']=$res1[$k]['app_name'];
	  $list1[$k]['remark']=$res1[$k]['remark'];
	  $list1[$k]['price']=$res1[$k]['price'];
    }
    $res2= D('use_return')->where(array('info_status'=>1,'name'=>$name,'specification'=>$specification,'unit'=>$unit,'return_num'=>'0','belongs_department'=>$belongs_department))->select();
	foreach($res2 as $k => $v){
	  $list2[$k]['status']='领用';
	  $list2[$k]['time']=$res2[$k]['use_time'];
	  $list2[$k]['name']=$res2[$k]['name'];
	  $list2[$k]['specification']=$res2[$k]['specification'];
	  $list2[$k]['unit']=$res2[$k]['unit'];
	  $list2[$k]['num']=$res2[$k]['use_num'];
	  $list2[$k]['app_name']=$res2[$k]['app_name'];
	  $list2[$k]['remark']=$res2[$k]['remark'];
	}
	$res3= D('use_return')->where(array('info_status'=>1,'name'=>$name,'specification'=>$specification,'unit'=>$unit,'use_num'=>'0','belongs_department'=>$belongs_department))->select();
	foreach($res3 as $k => $v){
	  $list3[$k]['status']='归还';
	  $list3[$k]['time']=$res3[$k]['return_time'];
	  $list3[$k]['name']=$res3[$k]['name'];
	  $list3[$k]['specification']=$res3[$k]['specification'];
	  $list3[$k]['unit']=$res3[$k]['unit'];
	  $list3[$k]['num']=$res3[$k]['return_num'];
	  $list3[$k]['app_name']=$res3[$k]['app_name'];
	  $list3[$k]['remark']=$res3[$k]['remark'];
	}
	$res4= D('application_form')->where(array('info_status'=>1,'name'=>$name,'specification'=>$specification,'unit'=>$unit,'type'=>'2','belongs_department'=>$belongs_department,'app_status'=>1))->select();
	foreach($res4 as $k => $v){
	  $list4[$k]['status']='报修';
	  $list4[$k]['time']=$res4[$k]['approval_time'];
	  $list4[$k]['name']=$res4[$k]['name'];
	  $list4[$k]['specification']=$res4[$k]['specification'];
	  $list4[$k]['unit']=$res4[$k]['unit'];
	  $list4[$k]['num']=$res4[$k]['num'];
	  $list4[$k]['app_name']=$res4[$k]['app_name'];
	  $list4[$k]['remark']=$res4[$k]['remark'];
	}
	$res5= D('application_form')->where(array('info_status'=>1,'name'=>$name,'specification'=>$specification,'unit'=>$unit,'type'=>'3','belongs_department'=>$belongs_department,'app_status'=>1))->select();
	foreach($res5 as $k => $v){
	  $list5[$k]['status']='报废';
	  $list5[$k]['time']=$res5[$k]['approval_time'];
	  $list5[$k]['name']=$res5[$k]['name'];
	  $list5[$k]['specification']=$res5[$k]['specification'];
	  $list5[$k]['unit']=$res5[$k]['unit'];
	  $list5[$k]['num']=$res5[$k]['num'];
	  $list5[$k]['app_name']=$res5[$k]['app_name'];
	  $list5[$k]['remark']=$res5[$k]['remark'];
	}
	$list = $list1;
	if($list2 && $list3 && $list4 && $list5){
		$list = array_merge($list1,$list2,$list3,$list4,$list5);
	}
	if($list2 && $list3 && $list4 && !$list5){
		$list = array_merge($list1,$list2,$list3,$list4);
	}
	if($list2 && $list3 && !$list4 && $list5){
		$list = array_merge($list1,$list2,$list3,$list5);
	}
	if($list2 && !$list3 && $list4 && $list5){
		$list = array_merge($list1,$list2,$list4,$list5);
	}
	if(!$list2 && $list3 && $list4 && $list5){
		$list = array_merge($list1,$list4,$list3,$list5);
	}
	if($list2 && $list3 && !$list4 && !$list5){
		$list = array_merge($list1,$list2,$list3);
	}
	if($list2 && !$list3 && $list4 && !$list5){
		$list = array_merge($list1,$list2,$list4);
	}
	if(!$list2 && $list3 && $list4 && !$list5){
		$list = array_merge($list1,$list3,$list4);
	}
	if($list2 && !$list3 && !$list4 && !$list5){
		$list = array_merge($list1,$list2);
	}
	if(!$list2 && $list3 && !$list4 && !$list5){
		$list = array_merge($list1,$list3);
	}
	if(!$list2 && !$list3 && $list4 && !$list5){
		$list = array_merge($list1,$list4);
	}
	if(!$list2 && !$list3 && !$list4 && $list5){
		$list = array_merge($list1,$list5);
	}
	if($list2 && !$list3 && !$list4 && $list5){
		$list = array_merge($list1,$list5,$list2);
	}
	if(!$list2 && $list3 && $list4 && !$list5){
		$list = array_merge($list1,$list5,$list3);
	}
	$result = D('office_stationery')->where(array('info_status'=>1,'name'=>$name,'specification'=>$specification,'unit'=>$unit,'belongs_department'=>$belongs_department))->select();
	foreach($list as $k => $v){
		foreach($result as $j =>$val){
			$list[$k]['type'] = $result[$j]['type'];
		}
	}
	
	$datetime = array();
	$count = 0;
	foreach ($list as $key=> $user) {
		$datetime[] = $user['time'];
		$count++;
	}
	array_multisort($datetime,SORT_DESC,$list);
	
	//$af_list = array_slice($list,($p-1)*8,8,true);
	
	if($start1 && $start2){
		$d = date('Y-m-d',strtotime($start2)+86400);
		foreach ($list as $key=>$val){
			if($start1 > $list[$key]['time']  ||  $list[$key]['time']>$d){
				unset($list[$key]);
				$count--;
			}
		}
	}
	$af_list = array_slice($list,($p-1)*8,8,true);
	
	
	$this->ajaxReturn(array('list'=>$af_list,'count'=>$count));
  }














}